import http from '../../../utils/https.js';

Page({
  data: {
    parkingSpaceId: '',
    parkingSpaceInfo: null,
    loading: true,
    currentStep: 2 // 默认先设为物业审核步骤
  },

  onLoad(options) {
    console.log('车位详情页面加载，参数:', options);
    
    if (options.id) {
      this.setData({
        parkingSpaceId: options.id
      });
      
      // 获取车位详细信息
      this.getParkingSpaceDetails(options.id);
      
      // 根据状态设置步骤
      if (options.status) {
        this.setStepByStatus(options.status);
      }
    } else {
      wx.showToast({
        title: '车位ID参数缺失',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  // 根据状态设置步骤
  setStepByStatus(status) {
    let currentStep = 2; // 默认物业审核步骤
    
    switch (parseInt(status)) {
      case 0: // 审核成功
        currentStep = 3;
        break;
      case 1: // 审核中
        currentStep = 2;
        break;
      case 2: // 审核未通过
        currentStep = 1;
        break;
      default:
        currentStep = 2;
    }
    
    this.setData({ currentStep });
  },

  // 获取车位详细信息
  getParkingSpaceDetails(parkingSpaceId) {
    console.log('获取车位详细信息，ID:', parkingSpaceId);
    
    this.setData({ loading: true });
    
    // 调用车位详情接口
    http.get(`parkingSpace/parkingSpace/${parkingSpaceId}`)
      .then(res => {
        console.log('车位详情接口返回:', res);
        
        if (res.code === 200 && res.data) {
          this.setData({
            parkingSpaceInfo: res.data,
            loading: false
          });
          
          console.log('车位信息设置完成:', this.data.parkingSpaceInfo);
        } else {
          console.log('获取车位详情失败:', res);
          this.setData({ loading: false });
          wx.showToast({
            title: '获取车位详情失败',
            icon: 'none'
          });
        }
      })
      .catch(err => {
        console.error('获取车位详情失败:', err);
        this.setData({ loading: false });
        wx.showToast({
          title: '获取车位详情失败',
          icon: 'none'
        });
      });
  },

  // 返回上一页
  goBack() {
    wx.navigateBack();
  },

  // 预览车辆图片
  previewVehicleImage(e) {
    const imageUrl = e.currentTarget.dataset.url;
    if (imageUrl) {
      wx.previewImage({
        urls: [imageUrl],
        current: imageUrl
      });
    }
  }

});